package com.agentlab.imitation.web.filter.admin;


import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.lang.xwork.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import sun.util.logging.resources.logging;

import com.agentlab.imitation.bean.admin.Admin;
import com.agentlab.imitation.common.CommonConstant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class PrivilegeFilter extends AbstractInterceptor{
	private static final long serialVersionUID = 598018707334253449L;
//	private RoleService roleservice;
//	  public void init() {
//		  try {
//				ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
//				 roleservice = (RoleService) act.getBean("roleServiceBean");
//			} catch (RuntimeException e) {
//				System.out.println("拦截器初始化错误.");
//				e.printStackTrace();
//				
//			}
//	    }
	/*
	 * Action处理的拦截方法 
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ctx= invocation.getInvocationContext(); 
		Map session = ctx.getSession();
		Admin user = (Admin)session.get("adminuser");
		//如果没有登陆 都返回重新登陆 
		String tourl = "";
		tourl = setGoingToURL(session,invocation);
		session.put(CommonConstant.GOING_TO_URL_KEY,tourl);
		if(user != null){
			return invocation.invoke();
//			if(before(invocation,user) == "false"){
//				return "showresult";
//			}else{
//				return invocation.invoke();
//			}
		}
		return "adminnopermission";
	}
	/**
	 * 权限校验
	 * @return
	 */
//	public  String  before(ActionInvocation invocation,OrgUsers user) throws Exception{
//		Method method = null ;
//		try {
//			method = invocation.getAction().getClass().getMethod(invocation.getProxy().getMethod());
//			if(method!=null && method.isAnnotationPresent(Permission.class)){
//				Permission permission = method.getAnnotation(Permission.class);//得到方法上的注解
//				//下面是得到执行方法需要的权限
//				SystemprivilegeId methodPrivilege = new SystemprivilegeId(permission.module(),permission.privilege());
//				List<SystemprivilegeId> spids = roleservice.queryURSystempId(user.getUid());
//				for(SystemprivilegeId spid : spids){
//					if(spid.equals(methodPrivilege)){
//						return "true";
//					}
//				}
//			}
//		} catch (Exception e) {
//			System.out.println("权限校验出错");
//		}
//		HttpServletRequest req = ServletActionContext.getRequest();
//		req.setAttribute("message", "您没有执行该操作的权限");		
//		return "false";
//	}
	
	@SuppressWarnings("unchecked")
	private String setGoingToURL(Map sessions, ActionInvocation invocation){
		String url = "";
		String namespace = invocation.getProxy().getNamespace();
		if (StringUtils.isNotBlank(namespace) && !namespace.equals("/")){
			url = url + namespace;
		}
		String actionName = invocation.getProxy().getActionName();
		if (StringUtils.isNotBlank(actionName)){
			url = url + "/" + actionName + ".action";
		}
		//sessions(CommonConstant.GOING_TO_URL_KEY, url);
		return url;
		
		
	}
	
//	public RoleService getRoleservice() {
//		return roleservice;
//	}
//	public void setRoleservice(RoleService roleservice) {
//		this.roleservice = roleservice;
//	}
	
}
